package user

import (
	"center-data-platform/app/user/api/internal/svc"
	"center-data-platform/app/user/api/internal/types"
	"center-data-platform/app/user/rpc/userclient"
	"context"
	"encoding/json"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic {
	return &UserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserInfoLogic) UserInfo() (resp *types.UserInfoResponse, err error) {
	userId, _ := l.ctx.Value("user_id").(json.Number).Int64()
	user, err := l.svcCtx.UserRpc.Info(l.ctx, &userclient.UserInfoReq{Id: userId})
	if err != nil {
		return nil, err
	}
	return &types.UserInfoResponse{
		Id:          int(user.Id),
		Email:       user.Email,
		UserName:    user.UserName,
		Phone:       user.Phone,
		Avatar:      user.Avatar,
		Bio:         user.Bio,
		CreatedTime: user.CreatedTime,
		Status:      int(user.Status),
		Role:        int(user.Role),
	}, nil
}
